﻿using AutoTest.Domain;
using AutoTest.Framework;
using AutoTest.Framework.Module;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SqlSugar;

namespace AutoTest.Repository
{
    public class RepositoryModule : ProgrameModule
    {
        public override void PreInit(BootParams model)
        {
            model.Services.AddSingleton<ISqlSugarClient>(s =>
            {
                var sqlSugar = new SqlSugarScope(new ConnectionConfig()
                {
                    DbType = DbType.MySql,
                    ConnectionString = model.Configuration.GetConnectionString(AppConsts.MYSQL_CONNECTSTRING),
                    IsAutoCloseConnection = true,
                },
               db =>
               {
                   //单例参数配置，所有上下文生效
                   db.Aop.OnLogExecuting = (sql, pars) =>
                   {
                       Log.Debug(db.CurrentConnectionConfig.ConnectionString);
                       //获取作IOC作用域对象
                       Log.Debug(sql, pars);
                   };
               });
                return sqlSugar;
            });
        }
    }
}
